﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace MiniScrum.Extensions
{
    public static class MenuHelper
    {
        public static MvcHtmlString Menu(this HtmlHelper helper)
        {
            var sb = new StringBuilder();

            // Create opening unordered list tag
            sb.Append("<ul class='left'>");

            // Render each top level node
            var topLevelNodes = SiteMap.RootNode.ChildNodes;
            foreach (SiteMapNode node in topLevelNodes)
            {
                MiniScrum.Models.User user = (System.Threading.Thread.CurrentPrincipal as MiniScrum.Models.User);
                //TODO: This is a bit cheesy
                if (node.Roles != null && node.Roles.Count == 1 && !user.IsInRole(node.Roles[0].ToString()))
                    continue;
                if (SiteMap.CurrentNode == node)
                    sb.AppendLine("<li class='active'>");
                else
                    sb.AppendLine("<li>");

                sb.AppendFormat("<a href='{0}'>{1}</a>", node.Url, helper.Encode(node.Title));
                sb.AppendLine("</li>");

            }

            // Close unordered list tag
            sb.Append("</ul>");

            return MvcHtmlString.Create(sb.ToString());
        }

    }

}